package com.hh.demo_ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.hh.adapters.CDTListAdapter;
import com.hh.clientdatatable.ClientDataTable;
import com.hh.clientdatatable.TCell.ValueType;
import com.hh.clientdatatable.TColumn;
import com.hh.listeners.OnDateSetListener;
import com.hh.listeners.OnPickColorListener;
import com.hh.listeners.OnSelectedDateListener;
import com.hh.ui.UiUtility;
import com.hh.ui.dialog.UiDialogCalender;
import com.hh.ui.dialog.UiDialogColorPicker;
import com.hh.ui.dialog.UiDialogDatePicker;
import com.hh.utility.PuDate;
import com.hh.utility.PuUtils;

public class Act_UiSamples extends Activity {

	private ClientDataTable _mCDT;
	private int _mSelectedColor;
	private CDTListAdapter _mAdapter ;
	private UiDialogColorPicker _mDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);


		createCDT();
		fillCDT();

		setContentView(R.layout.act_ui_samples);

		ListView lList=(ListView) findViewById(R.id.mListSamples);
		_mDialog=new UiDialogColorPicker(Act_UiSamples.this);
		_mSelectedColor=Color.YELLOW;
		_mDialog.setColor(_mSelectedColor);
		_mAdapter=new CDTListAdapter(this, R.layout.rowlv, _mCDT){	

			@Override
			protected void onIteratedRow(View row, View widget,int position) {
				super.onIteratedRow(row, widget,position);

				if(widget.getTag().toString().equals("FrameColor")){
					FrameLayout lFrame=(FrameLayout) widget;
					if(position==9){
						lFrame.setVisibility(View.VISIBLE);
						lFrame.setBackgroundColor(_mSelectedColor);
					}else
						lFrame.setVisibility(View.GONE);
				}

			}
			@Override
			protected void onClickRow(View row, int position) {
				super.onClickRow(row, position);

				switch (position) {

				// PopupMenu
				case 0:
					startActivity(new Intent(Act_UiSamples.this,Sample_PopupMenu.class));
					break;
					//Action Bar
				case 1:
					startActivity(new Intent(Act_UiSamples.this,Sample_ActionBar.class));
					break;
					// EditText
				case 2:
					startActivity(new Intent(Act_UiSamples.this,Sample_EditText.class));
					break;
					// Tab Host
				case 3:
					startActivity(new Intent(Act_UiSamples.this,Sample_TabHost.class));
					break;
					// Spinner
				case 4:
					startActivity(new Intent(Act_UiSamples.this,Sample_Spinner.class));
					break;
					// Left sliding menu
				case 5:
					startActivity(new Intent(Act_UiSamples.this,Sample_LeftSlidingMenu.class));
					break;
					// Alert Dialog
				case 6:
					startActivity(new Intent(Act_UiSamples.this,Sample_Dialog.class));
					break;
					// DatePicker dialog
				case 7:
					PuDate lToDay=PuUtils.getCurrentDate(mContext);
					UiDialogDatePicker lDatePicker=new UiDialogDatePicker(mContext, lToDay.day, lToDay.month, lToDay.year);
					lDatePicker.setOnDateSetListener(new OnDateSetListener() {
						@Override
						public void onDateSetClick(int dayOfMonth,int monthOfYear, int year) {
						
							String lDate=""+dayOfMonth+"/"+monthOfYear+"/"+year;
							Toast.makeText(mContext, "Selected Date :"+lDate, Toast.LENGTH_SHORT).show();
						}				
					});
					lDatePicker.show();
					break;
					// Calendar Dialog
				case 8:
					
					// get current date
					PuDate lToDate=PuUtils.getCurrentDate(mContext);
					UiDialogCalender lDialogCald =new UiDialogCalender(mContext,lToDate.day,lToDate.month,lToDate.year);
					
					// fix the color of the current date
					lDialogCald.setCurrentDateColor(Color.RED);
					
					lDialogCald.setOnSelectedDateListener(new OnSelectedDateListener() {			
						@Override
						public void OnGetSelectedDate(int pDay, int pMonth, int pYear) {
							
							String lDate=""+pDay+"/"+pMonth+"/"+pYear;
							Toast.makeText(mContext, "Selected Date :"+lDate, Toast.LENGTH_SHORT).show();
						}
					});
					lDialogCald.show();
					break;
					// ColorPicker Dialog
				case 9:
					_mDialog.setOnPickColor(new OnPickColorListener() {		
						@Override
						public void onPick(int color) {
							// display selected color on integer format and Hex format
							Toast.makeText(Act_UiSamples.this, color+" Hex :"+UiUtility.colorConvertIntegerToHex(color), Toast.LENGTH_SHORT).show();
							_mSelectedColor=color;
							_mAdapter.notifyDataSetChanged();
						}
					});
					_mDialog.show();
					break;
					// Horizontal listView
				case 10:
					startActivity(new Intent(Act_UiSamples.this,Sample_HorizontalListView.class));
					break;
					// Navigation Action Bar
				case 11:
					startActivity(new Intent(Act_UiSamples.this,Sample_NavigationBar.class));
					break;
				default:
					break;
				}
			}
		};
		lList.setAdapter(_mAdapter);
	}


	private void createCDT(){

		_mCDT=new ClientDataTable(this);
		_mCDT.addColumn(new TColumn("Item", ValueType.TEXT));
		_mCDT.addColumn(new TColumn("Desc", ValueType.TEXT));
		_mCDT.addColumn(new TColumn("IsColor", ValueType.BOOLEAN));
	}


	private void fillCDT(){

		String[] lMainTab=getResources().getStringArray(R.array.ArraySamplesCodes);
		String[] lDesTab=getResources().getStringArray(R.array.ArrayDescriptions);

		for (int i = 0; i < lDesTab.length; i++) {
			_mCDT.append();
			_mCDT.cellByName("Item").setValue(lMainTab[i]);
			_mCDT.cellByName("Desc").setValue(lDesTab[i]);

			if(i==9)
				_mCDT.cellByName("IsColor").setValue(true);
			else
				_mCDT.cellByName("IsColor").setValue(false);
			
			_mCDT.commit();
		}
	}

}
